﻿
using OnlineCourse.Model;
using OnlineCourse.Model.Entity;
using OnlineCourse.Services.IServices;
using OnlineCourse.Services.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OnlineCourse.Services
{
    public class CouponRecordService :ICouponsRecordService
    {
        private ApplicationDbContext _db;
        public CouponRecordService(ApplicationDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 添加领优惠券记录
        /// </summary>
        /// <param name="State">状态</param>
        /// <param name="CouponsId">券类型</param>
        /// <param name="UsersId">用户ID</param>
        /// <param name="Source">来源</param>
        /// <returns></returns>
        public bool Add(int num, out int meg, Guid CouponsId, string Source, Guid UserId)
        {
            try
            {           
                Coupon d = new Coupon();
                CouponsRecord c = new CouponsRecord();
                d = _db.Coupons.Where(p => p.Id==CouponsId).FirstOrDefault();
                if (d.Number<num)
                {
                    meg = 1;//券数量不够
                    return false;
                }
                else
                {
                    meg = 0;//券数量足够
                    c.State = 1;
                    c.CouponId = CouponsId;
                    c.Source = Source;
                    c.UserId = UserId;
                    _db.CouponsRecords.Add(c);
                    d.Number = d.Number - num;
                    _db.SaveChanges();
                    return true;
                }
            }
            catch (Exception error)
            {
                throw error;
            }
        }
        /// <summary>
        /// 更改状态
        /// </summary>
        public void State()
        {
            int State = 0;

            CouponsRecord c = new CouponsRecord();

            if (State == 0)
            {
                c.State = 0;//消费掉
            }
            else if (State == 1)
            {
                c.State = 1;//未使用
            }
        }
        public bool Update(Guid id, int State)
        {
            try
            {
                var a = _db.CouponsRecords.FirstOrDefault(p => p.Id== id);
                if (a!=null)
                {
                    a.State = State;
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception error)
            {
                throw error;
            }
        }

        /// <summary>
        /// 按照用户ID分页查询券清单
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<CouponsModel> GetUserId(Guid UserId, out int total, int page = 1, int pageSize = 10)
        {
            var list = _db.CouponsRecords.Where(b => b.UserId == UserId).Select(a => (new CouponsModel(){ CouponId = a.CouponId , UserId = a.UserId,Title=a.Coupon.Title,Price=a.Coupon.Price,Source=a.Source,Number=a.Coupon.Number,State=a.State,CreateDate=a.CreatedDate,UpdatedDate=a.UpdatedDate }));
            var list2 = list.Skip(pageSize * (page - 1)).Take(pageSize).ToList();
            total = list2.Count();
            return list2;
        }
        /// <summary>
        /// 按照券类型查询优惠券清单
        /// </summary>
        /// <param name="CouponId"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<CouponsModel> GetCouponId(Guid CouponId,out  int total, int page = 1, int pageSize = 10)
        {
            var list = _db.CouponsRecords.Where(b => b.CouponId == CouponId).Select(a => (new CouponsModel() { CouponId = a.CouponId, UserId = a.UserId, Title = a.Coupon.Title, Price = a.Coupon.Price, Source = a.Source, Number = a.Coupon.Number, State = a.State, CreateDate = a.CreatedDate, UpdatedDate = a.UpdatedDate }));
            var list2 = list.Skip(pageSize * (page - 1)).Take(pageSize).ToList();
            total = list2.Count();
            return list2;
        }
        /// <summary>
        /// 按照来源查询清单
        /// </summary>
        /// <param name="Source"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<CouponsModel> GetSource(String Source, out int total, int page = 1, int pageSize = 10)
        {
            var list = _db.CouponsRecords.Where(b => b.Source ==Source).Select(a => (new CouponsModel() { CouponId = a.CouponId, UserId = a.UserId, Title = a.Coupon.Title, Price = a.Coupon.Price, Source = a.Source, Number = a.Coupon.Number, State = a.State, CreateDate = a.CreatedDate, UpdatedDate = a.UpdatedDate }));
            var list2 = list.Skip(pageSize * (page - 1)).Take(pageSize).ToList();
            total = list2.Count();
            return list2;
        }
        /// <summary>
        /// 按照身份查询清单
        /// </summary>
        /// <param name="State"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<CouponsModel> GetState(int State, out int total, int page = 1, int pageSize = 10)
        {
            var list = _db.CouponsRecords.Where(b => b.State==State).Select(a => (new CouponsModel() { CouponId = a.CouponId, UserId = a.UserId, Title = a.Coupon.Title, Price = a.Coupon.Price, Source = a.Source, Number = a.Coupon.Number, State = a.State, CreateDate = a.CreatedDate, UpdatedDate = a.UpdatedDate }));
            var list2 = list.Skip(pageSize * (page - 1)).Take(pageSize).ToList();
            total = list2.Count();
            return list2;
        }
       
    }
}



